package service

import (
	"gin-vue-admin/global"
	"gin-vue-admin/model"
	"gin-vue-admin/model/request"
)

// CreateFakeDetectResult 创建FakeDetectResult记录
// Author [piexlmax](https://github.com/piexlmax)
func CreateFakeDetectResult(res model.FakeDetect) (err error) {
	err = global.GVA_DB.Create(&res).Error
	return err
}

// DeleteFakeDetectResult 删除FakeDetectResult记录
// Author [piexlmax](https://github.com/piexlmax)
func DeleteFakeDetectResult(res model.FakeDetect) (err error) {
	err = global.GVA_DB.Delete(&res).Error
	return err
}

// DeleteFakeDetectResultByIds 批量删除FakeDetectResult记录
// Author [piexlmax](https://github.com/piexlmax)
func DeleteFakeDetectResultByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]model.FakeDetect{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateFakeDetectResult 更新FakeDetectResult记录
// Author [piexlmax](https://github.com/piexlmax)
func UpdateFakeDetectResult(res model.FakeDetect) (err error) {
	err = global.GVA_DB.Save(&res).Error
	return err
}

// GetFakeDetectResult 根据id获取FakeDetectResult记录
// Author [piexlmax](https://github.com/piexlmax)
func GetFakeDetectResult(id uint) (err error, res model.FakeDetect) {
	err = global.GVA_DB.Preload("FakeDetectRes").Where("id = ?", id).First(&res).Error
	return
}

// GetFakeDetectResultInfoList 分页获取FakeDetectResult记录
// Author [piexlmax](https://github.com/piexlmax)
func GetFakeDetectResultInfoList(info request.FakeDetectResultSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&model.FakeDetect{})
	var ress []model.FakeDetect

	if info.ReqState != 0 {
		db = db.Where("`req_state` = ?", info.ReqState)
	}
	err = db.Count(&total).Error
	err = db.Limit(limit).Offset(offset).Find(&ress).Error
	return err, ress, total
}
